$(document).on('click','.logbtn',function log(){
	var name = $('#name').val();
	var pass = $('#pass').val();
	
	
	ajaxSubmit(name,pass);
});
var xmlHttpRequest = null;

function ajaxSubmit(name, pass){
//	window.alert(window.ActiveXObject);

	
	if(window.ActiveXObject){	//是IE浏览器

		xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");

	}else if(window.XMLHttpRequest){	//除IE外的其他浏览器

		xmlHttpRequest = new XMLHttpRequest();
	}

	/*
		使用Ajax准备向服务器端发送一个请求
	*/
	if(null != xmlHttpRequest){	//使用GET方式，			//异步
	
	//	xmlHttpRequest.open("GET", "AjaxServlet?num1=" + num1 + "&num2=" + num2, "true");

		xmlHttpRequest.onreadystatechange = ajaxCallBack;	//注册一个回调函数

		//向服务器端发送数据
	//	xmlHttpRequest.send(null);	//get方式设置为null

		//post方式
		xmlHttpRequest.open("POST", "LoginServlet", "true");
		//使用post方式提交数据，必须加上下面这行代码
		xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		xmlHttpRequest.send("name=" + name+"&pass="+pass);

	}
}

function ajaxCallBack(){

	if(xmlHttpRequest.readyState == 4){

		if(xmlHttpRequest.status == 200){
			var responseText = xmlHttpRequest.responseText;	
			
		//	document.getElementById("content").innerText = responseText;	
		// alert(responseText);
		responseText = responseText.trim();
		if(responseText == "fail"){
			$('#error').css('display','block');	
		}
		 else{
			 $('#error').css('display','none');	
			 var name = $('#name').val();
			 setCookie('username',name);
			 alert('登录成功！');
			window.location.href="index.html";
		} 
			
		}
	} 
}

function setCookie(name,value)
{
/*
*--------------- setCookie(name,value) -----------------
* setCookie(name,value)
* 功能:设置得变量name的值
* 参数:name,字符串;value,字符串.
* 实例:setCookie('username','baobao')
*--------------- setCookie(name,value) -----------------
*/
var Days = 30; //此 cookie 将被保存 30 天
 var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) ;
//location.href = "index.html"; //接收页面.
}
